package cn.myfirefly.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FileUtil {

	public static String getWebPath() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		File file = new File(path);
		File pFile = file.getParentFile();
		return pFile.getAbsolutePath();
	}
	
	/**
	 * 获取目录下文件，并按照最后修改时间排序
	 * @param filePath 文件目录
	 * @param includeSub 是否包含子目录的文件，true：包含，false：不包含
	 * @param sortType 0：升序，1：降序
	 * @return
	 */
	public static File[] getFilesSortByModifyTime(String filePath, boolean includeSub, int sortType) {
		File root = new File(filePath);
		if (!root.exists()) {
			return null;
		}
		File[] files = root.listFiles();
		
		if (includeSub) {
			List<File> fileList = new ArrayList<>();
			for (File file : files) {
				getFiles(file, fileList);
			}
			files = new File[fileList.size()];
			files = fileList.toArray(files);
			Arrays.sort(files, new FileUtil.CompratorByLastModified(sortType));
			return files;
		} else {
			List<File> fileList = new ArrayList<>();
			for (File file : files) {
				if (!file.isDirectory()) {
					fileList.add(file);
				}
			}
			files = new File[fileList.size()];
			files = fileList.toArray(files);
			Arrays.sort(files, new FileUtil.CompratorByLastModified(sortType));
			return files;
		}
	}
	
	/**
	 * 获取目录下文件
	 * @param filePath 文件目录
	 * @param includeSub 是否包含子目录的文件，true：包含，false：不包含
	 * @return
	 */
	public static File[] getFiles(String filePath, boolean includeSub) {
		File root = new File(filePath);
		if (!root.exists()) {
			return null;
		}
		File[] files = root.listFiles();
		
		if (includeSub) {
			List<File> fileList = new ArrayList<>();
			for (File file : files) {
				getFiles(file, fileList);
			}
			files = new File[fileList.size()];
			files = fileList.toArray(files);
			return files;
		} else {
			List<File> fileList = new ArrayList<>();
			for (File file : files) {
				if (!file.isDirectory()) {
					fileList.add(file);
				}
			}
			files = new File[fileList.size()];
			files = fileList.toArray(files);
			return files;
		}
	}
	
	/**
	 * 获取目录下所有文件
	 * @param file
	 * @param fileList
	 */
	private static void getFiles(File file, List<File> fileList) {
		if (file.isDirectory()) {
            File[] subfiles = file.listFiles();
            for (File subFile : subfiles) {
                if (subFile.isDirectory()) {
                    getFiles(subFile, fileList);
                } else {
                	fileList.add(subFile);
                }
            }
        } else {
        	fileList.add(file);
		}
	}
	
	/**
	 * 根据文件修改时间进行比较的内部类
	 * 
	 * @author zhouxiaoming
	 * @date 2018年10月19日
	 */
	static class CompratorByLastModified implements Comparator<File> {
		private int sortType = 0;
		public CompratorByLastModified(int sortType) {
			this.sortType = sortType;
		}
		
		public int compare(File f1, File f2) {  
            long diff = f1.lastModified() - f2.lastModified();
            if (sortType == 0) {
            	if (diff > 0) {  
            		return 1;  
            	} else if (diff == 0) {  
            		return 0;  
            	} else {  
            		return -1;  
            	}  
			} else {
				if (diff > 0) {  
            		return -1;  
            	} else if (diff == 0) {  
            		return 0;  
            	} else {  
            		return 1;  
            	}  
			}
        }  
	}
	
	public static void main(String[] args) {
		File[] files = FileUtil.getFilesSortByModifyTime(FileUtil.getWebPath(), false, 1);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
	}
}
